package org.matchit.matcher.text;

import java.util.regex.Pattern;

import org.hamcrest.CustomTypeSafeMatcher;
import org.hamcrest.Factory;

public class StringPatternMatcher extends CustomTypeSafeMatcher<CharSequence> {
    private final String regex;

    /**
     * Construct a new {@link StringPatternMatcher}.
     * @param regex the pattern that should be matched
     */
    public StringPatternMatcher(final String regex) {
        super("matches(" + regex + ")");
        this.regex = regex;
    }

    @Factory
    public static StringPatternMatcher pattern(String regex) {
        return new StringPatternMatcher(regex);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean matchesSafely(CharSequence text) {
        return Pattern.matches(regex, text);
    }

}
